package com.xgkj.diyiketang.util;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.DecimalFormat;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;

import com.xgkj.diyiketang.EatShowApp;
import com.xgkj.diyiketang.R;



/**
 * view工具类
 * @author houmiao.xiong
 * @date 2012-8-9
 */
public class ViewUtil {

	public static final String TAG = "ViewUtil";

	static int screenHeight;		//屏幕高度
	static int screenWidth;			//屏幕宽度
	static float screenDensity;		//屏幕分辨率
	static ProgressDialog mProgressDialog;


	/**
	 * 获得屏幕大小
	 * @param acti
	 */
	public static void getScreenSize(Activity acti){
		if(screenDensity==0||screenWidth==0||screenHeight==0){
			DisplayMetrics dm = new DisplayMetrics();
			acti.getWindowManager().getDefaultDisplay().getMetrics(dm);
			screenDensity = dm.density;
			screenHeight = dm.heightPixels;
			screenWidth = dm.widthPixels;
		}
	}

	/**
	 * 获得屏幕宽度
	 * @author houmiao.xiong
	 * @param context
	 * @return
	 */
	public static int getScreenWidth(Context context){
		if(screenWidth == 0){
            if(context == null){
                throw new NullPointerException("context can not be null");
            }else if(context instanceof Activity){
				DisplayMetrics dm = new DisplayMetrics();
				((Activity)context).getWindowManager().getDefaultDisplay().getMetrics(dm);
				screenWidth = dm.widthPixels;
			}else{
				throw new IllegalArgumentException("context must be instanceof Activity");
			}
		}

		return screenWidth;
	}

	/**
	 * 获得屏幕高度
	 * @author houmiao.xiong
	 * @param context
	 * @return
	 */
	public static int getScreenHeight(Context context){
		if(screenHeight == 0){
            if(context == null){
                throw new NullPointerException("context can not be null");
            } else if(context instanceof Activity){
				DisplayMetrics dm = new DisplayMetrics();
				((Activity)context).getWindowManager().getDefaultDisplay().getMetrics(dm);
				screenHeight = dm.heightPixels;
			} else {
				throw new IllegalArgumentException("context must be instanceof Activity");
			}
		}

		return screenHeight;
	}

	/**
	 * 获得屏幕密度
	 * @author houmiao.xiong
	 * @param context
	 * @return
	 */
	public static float getScreenDesity(Context context){
		if(screenDensity == 0){
            if(context == null){
                throw new NullPointerException("context can not be null");
            } else if(context instanceof Activity){
				DisplayMetrics dm = new DisplayMetrics();
				((Activity)context).getWindowManager().getDefaultDisplay().getMetrics(dm);
				screenDensity = dm.density;
			} else {
				throw new IllegalArgumentException("context must be instanceof Activity");
			}
		}

		return screenDensity;
	}

	
	
	

	
	public static Size getImageSize(Context context,int resID){
		Bitmap defMarketBitmap = BitmapFactory.decodeResource(context.getResources(),resID);
		Size size = new Size(defMarketBitmap.getWidth(),defMarketBitmap.getHeight());
		defMarketBitmap.recycle();
		return size;
	}




	

	/**
	 * 设置图片最大大小
	 * @param view
	 * @param defaultBitmap
	 */
	public static void setViewMaxSize(ImageView view,Bitmap defaultBitmap){
		view.setAdjustViewBounds(true);
		view.setMaxWidth(defaultBitmap.getWidth());
		view.setMaxHeight(defaultBitmap.getHeight());
//		view.setMinimumHeight(defaultBitmap.getHeight());
//		view.setMinimumWidth(defaultBitmap.getWidth());
	}
	
	/**
	 * 设置图片最大大小
	 * @param view
	 * @param height
	 */
	public static void setViewMaxSize(ImageView view,int width,int height){
		view.setAdjustViewBounds(true);
		view.setMaxWidth(width);
		view.setMaxHeight(height);
//		view.setMinimumHeight(defaultBitmap.getHeight());
//		view.setMinimumWidth(defaultBitmap.getWidth());
	}

	/**
	 * 设置bitmap为固定图片的大小
	 * @param defaultBitmap
	 */
	public static Bitmap setBitmapSize(Bitmap defaultBitmap,Bitmap destBitmap){
		
		 return Bitmap.createScaledBitmap(destBitmap, defaultBitmap.getWidth(), defaultBitmap.getHeight(), false);
	}

	/**
	 * 缩放Bitmap
	 * @param bitmap
	 * @param multiple
	 * @return
	 */
	public static Bitmap scaleBitmap(Bitmap bitmap,double multiple){
		return Bitmap.createScaledBitmap(bitmap,(int)(bitmap.getWidth()*multiple),
				(int)(bitmap.getHeight()*multiple), true);
	}

	/**
	 * px转dip
	 * @param context
	 * @param pxValue
	 * @return
	 */
	public static int px2dip(Context context, float pxValue) {
		return (int) (pxValue / getScreenDesity(context) + 0.5f);
	}


	/**
	 * dip转px
	 * @param context
	 * @param dipValue
	 * @return
	 */
	public static int dip2px(Context context, float dipValue) {
		return (int) (dipValue * getScreenDesity(context) + 0.5f);
	}
	
	public static Size getHeadImageSize(Context context){
		return new Size(dip2px(context, 60),dip2px(context, 60));
	}

	public static Size getHeadImageSize_40(Context context){
		return new Size(dip2px(context, 40),dip2px(context, 40));
	}

	public static Size getHeadImageSize(Context context,int size){
		return new Size(dip2px(context,size),dip2px(context,size));
	}

	public static Size getHeadImageSize_375(Context context){
		return new Size(dip2px(context, 360),dip2px(context, 360));
	}
	public static Size getFengmianImageSize_375(Context context){
		return new Size(dip2px(context, getScreenWidth(context)),dip2px(context, getScreenWidth(context)));
	}
	// 

	/**
	 * shouli.luo
	 * @param file
	 * @return 按图片大小(字节大小)缩放图片
	 */
	public static Bitmap fitSizeImg(File file) {
		Bitmap resizeBmp = null;
		BitmapFactory.Options opts = new BitmapFactory.Options();
		
		// 数字越大读出的图片占用的heap越小 不然总是溢出
		if (file.length() < 20480) {       // 0-20k
			opts.inSampleSize = 1;
		} else if (file.length() < 51200) { // 20-50k
			opts.inSampleSize = 2;
		} else if (file.length() < 307200) { // 50-300k
			opts.inSampleSize = 4;
		} else if (file.length() < 819200) { // 300-800k
			opts.inSampleSize = 6;
		} else if (file.length() < 1048576) { // 800-1024k
			opts.inSampleSize = 8;
		} else {
			opts.inSampleSize = 10;
		}
		resizeBmp = BitmapFactory.decodeFile(file.getPath(), opts);
		return resizeBmp;
	}


	/**
	 * shouli.luo
	 * @param listView
	 * 根据item的高度确定listView的高度
	 */
	public static void setListViewHeightBasedOnChildren(ListView listView,BaseAdapter listAdapter) {

		if (listAdapter == null)
		{
			return;
		}
		int totalHeight = 0;
		for (int i = 0, len = listAdapter.getCount(); i < len; i++)
		{
			View listItem = listAdapter.getView(i, null, listView);
			listItem.measure(0, 0);
			totalHeight += listItem.getMeasuredHeight();
		}
		ViewGroup.LayoutParams params = listView.getLayoutParams();
		params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)) + listView.getPaddingTop() + listView.getPaddingBottom();
		listView.setLayoutParams(params);

	}
	
	public static class Size{
		public int width;

		public int height;


		public Size(int w,int h){
			width = w;
			height = h;
		}
	}

	public static String StringChange(String s){
		DecimalFormat format = new DecimalFormat("0.00");
		double oldPrice_d = 0.0;
		try {
			oldPrice_d = Double.parseDouble(s);
		} catch (Exception e) {
			e.printStackTrace();
		}
		String oldPrice_s = format.format(oldPrice_d);
		return oldPrice_s;
	}

	public static Bitmap revitionImageSize(String path) throws IOException {
		BufferedInputStream in = new BufferedInputStream(new FileInputStream(
				new File(path)));
		BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = true;
		BitmapFactory.decodeStream(in, null, options);
		in.close();
		int i = 0;
		Bitmap bitmap = null;
		while (true) {
			if ((options.outWidth >> i <= 1000)
					&& (options.outHeight >> i <= 1000)) {
				in = new BufferedInputStream(
						new FileInputStream(new File(path)));
				options.inSampleSize = (int) Math.pow(2.0D, i);
				options.inJustDecodeBounds = false;
				bitmap = BitmapFactory.decodeStream(in, null, options);
				break;
			}
			i += 1;
		}
		return bitmap;
	}

	public static boolean isNull(String s){
//		if (s == null) return true;
//		if (s.length() ==0) return true;
//		if (s.trim().length() ==0) return true;
//		return false;
		String mid_s = null;
		if (s != null){
			mid_s = s.trim();
		}

		if (TextUtils.isEmpty(mid_s)){
			return true;
		}

		return false;
	}

	public static void showProgressDialog(Context mContext,String promt, final int taskId){
		if (mProgressDialog == null || !mProgressDialog.isShowing()){
			mProgressDialog = DialogUtil.showProgressDialog(mContext, promt, new DialogInterface.OnCancelListener() {
				@Override
				public void onCancel(DialogInterface dialog) {
					EatShowApp.getInstance().getTaskManager().cancelTask(taskId);
					mProgressDialog = null;
				}
			});
		}
	}

	public static void dismissDialog(){
		if (mProgressDialog != null && mProgressDialog.isShowing()){
			mProgressDialog.dismiss();
			mProgressDialog = null;
		}
	}


}
